package com.neuedu.elmserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.elmserver.pojo.Address;
import com.neuedu.elmserver.mapper.AddressMapper;
import com.neuedu.elmserver.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.elmserver.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 收货地址 服务实现类
 * </p>
 *
 * @author wyh
 * @since 2024-11-11
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 新增收货地址
     * @param address
     * @return
     */
    @Override
    public ResultVO saveAddress(Address address) {
        addressMapper.insert(address);
        return ResultVO.ok("新增成功");
    }

    /**
     * 查询指定用户的收货地址列表
     * @param userId
     * @return
     */
    @Override
    public ResultVO queryAddressByUser(Integer userId) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",userId);
        return ResultVO.ok("查询成功",addressMapper.selectList(qw));
    }

    /**
     * 更新收货地址
     * @param address
     * @return
     * @RequestBody:将请求体(请求参数)的JSON数据类型转为Java对象
     */
    @Override
    public ResultVO editAddress(Address address) {
        UpdateWrapper uw=new UpdateWrapper();//增删改的条件构造器
        uw.set("address", address.getAddress());
        uw.set("contact_name", address.getContactName());
        uw.set("contact_phone", address.getContactPhone());
        uw.set("contact_sex", address.getContactSex());
        uw.eq("user_id",address.getUserId());
        uw.eq("address_id",address.getAddressId());
        //System.out.println(address.getUserId());
        //System.out.println(address.getAddressId());
        //System.out.println(address.getAddress());
        addressMapper.update(null,uw);
        return ResultVO.ok("更新成功");
    }

    /**
     * 删除收货地址
     * @param address
     * @return
     */
    @Override
    public ResultVO deleteAddress(Address address) {
        UpdateWrapper uw=new UpdateWrapper();//增删改的条件构造器
        uw.eq("address_id",address.getAddressId());
        addressMapper.delete(uw);
        return ResultVO.ok("删除成功");
    }
}
